
/***************************************************************************
 *   Copyright (C) 1997 to 2004 by Jonathan Duddington                     *
 *   email: jonsd@users.sourceforge.net                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write see:                           *
 *               <http://www.gnu.org/licenses/>.                           *
 ***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "flex.h"
#include "werr.h"
#include "dbox.h"

#include "narc.h"
#include "hdrs.h"

#ifdef MemCheck
#include "MemCheck:Flex.h"
#endif


extern BOOL dbox_import_raw_handler(dbox d, void *event, void *handle);
extern int box_lookup_number(int number);
extern char *make_legal_fname(char *user_id);
extern char *get_user_id(OPTIONS_MAILBOX *user);

extern FOLDREC cty_fr;
extern FOLDREC src_fr;
extern dbox dbox_import;

extern char *path_choices;
extern OPTIONS options;
extern CARDFILE_REC cardfile[];

static FILE *f_import;
static int import_date;




extern char *wipe_command;



/* Import from KO2 */


static char *query_parent = "Import under a \"KO2\" parent?";

void dbox_import_auto_handler(dbox d, void *handle)
/*************************************************/
{

	char *p;
	char *p2;
	int  n;
	int  c;
	int  cat;
	long int displ_start;
	long int displ_end;
	long int displ_next;
	int text_length;
	long int file_length;
	int file_count=0;
	long int  displ;
	int  box;
	FILE *f;

	char *text_buf;
	int  text_buf_length=0;

	CARD_EXPANDED card_ex;

	char buf[128];

	if(check_lock_lists(0xffff) != 0)
		return;

	displ = ftell(f_import);
	memset(&card_ex,0,sizeof(card_ex));

	box = options.text_box;

	if(dbox_get(d) != 1)
	{
		dbox_dispose(&dbox_import);
		dbox_import = NULL;
		return;
	}

	dbox_getfield(d,2,buf,sizeof(buf));
	box = get_box_number(buf);
	dbox_dispose(&dbox_import);
	dbox_import = NULL;

	f = f_import;

	/* divide textfile into segments, for each article file */
	displ_start = ftell(f);
	fseek(f,0,SEEK_END);
	displ_end = ftell(f);
	fseek(f,displ_start,SEEK_SET);
	file_length = (displ_end - displ_start) / N_ART_FILES;

	article_open_shortest(-1,0,-1);

	while(!feof(f))
	{
		/* distribute text among the article files */
		displ = ftell(f);
		if(displ > (file_length * file_count))
		{
			article_open_shortest(-1,0,-1);
			file_count++;
		}

		if(fgets(buf,sizeof(buf),f) == NULL)
			break;

		if(memcmp(buf,"@Context ",9)==0)
		{
			p = &buf[9];
			n = strlen(buf)-1;
			buf[n]=0;  /* delete trailing /n */

			/* separate into individual context fields */
			while(p < &buf[n])
			{
				p2 = p;
				while((*p2 != 0) && (*p2 != '/'))
					p2++;
				*p2 = 0;

				cat = category_lookup(&cty_fr,p,0);
				if((cat > 0) && (card_ex.n_cats < 31))
				{
					card_ex.cat_codes[card_ex.n_cats++] = cat;
				}

				p = p2+1;
			}
		}
		else if(memcmp(buf,"@Source ",8)==0)
		{
			p = &buf[8];
			n = strlen(buf)-1;
			buf[n]=0;  /* delete trailing /n */

			card_ex.source = category_lookup(&src_fr,p,0);
		}
		else if(strcmp(buf,"@TEXTITEM\n")==0)
		{
			displ_start = (int)ftell(f);   /* start of text */
			displ_end = displ_start;
			while(fgets(buf,sizeof(buf),f) != NULL)
			{
				if(strcmp(buf,"@TEXTEND\n")==0)
					break;

				displ_end = (int)ftell(f);
			}
			displ_next = ftell(f);
			text_length = (int)(displ_end - displ_start);

			if(text_length > text_buf_length)
			{
				if(flex_alloc((flex_ptr)&text_buf,text_length + TEXT_EXTRA)==0)
				{
					werr(0,"Can't allocate memory for text");
					text_buf_length = 0;
					return;
				}
				text_buf_length = text_length + TEXT_EXTRA;
			}

			fseek(f,displ_start,SEEK_SET);
			fread(text_buf,text_length,1,f);
			fseek(f,displ_next,SEEK_SET);

			/* add this text item */
			card_ex.text_anchor = &text_buf;
			card_ex.text_start = 0;
			card_ex.text_length = text_length;
			card_ex.status = 2;   /* been read */
			card_ex.filetype = 0;
			card_ex.date = import_date;
			card_ex.docbox = box;

			p = text_buf;
			p2 = card_ex.title;
			for(n=0; n<50; n++)
			{
				c = p2[n] = p[n];
				if(c <= '\n')
					break;
			}
			p2[n] = 0;


			if(cardfile_add(&card_ex) == NULL)
			{
				werr(0,"Import abandoned");
			}

			/* reset source and category data */
			memset(&card_ex,0,sizeof(card_ex));
		}
		else if(buf[0]=='@')
		{
			/* read additional field into 'comment' */
		}
	}

	if(text_buf_length > 0)
	{
		flex_free((flex_ptr)&text_buf);
	}
	article_file_close();
	redraw_list_lines(NULL,0);
	fclose(f_import);
}   /* dbox_import_auto_handler */



void import_auto_text(FILE *f, char *fname)
/*****************************************/
{
	static long int  i;

	import_date = get_time_stamp(fname);
	f_import = f;
	i = ftell(f_import);

	dbox_import = dbox_new("Import");
	dbox_fadefield(dbox_import,3);
	dbox_setfield(dbox_import,2,get_box_name(options.text_box));

	dbox_raw_eventhandler(dbox_import,dbox_import_raw_handler,NULL);
	dbox_eventhandler(dbox_import, dbox_import_auto_handler, NULL);

	dbox_showstatic(dbox_import);
}   /* end of import_auto_text */







void import_cats(FOLDREC *fr, FILE *f, int pass)
/**********************************************/
{
	int  i;
	int  chain=0;
	static int  parent;
	int  cat;
	int  cat2;
	CAT *cptr;
	char *p;
	int  c;
	int  relation;
	char cat_name[20];
	char cat_name2[20];
	char cat_comment[120];
	char cat_hierarchy[120];
	char buf[120];


	if(pass==1)
	{
		parent = 0;
		if(query2(query_parent,"Yes","No")!=0)
		{
			strcpy(buf,"KO2");  /* needed because of program overlays */
			parent = category_lookup(fr,buf,0);
			if(parent == 0)
			{
				category_add_child(fr,buf,NULL,&parent,0);
				category_sort(fr,0);
			}
		}
	}

	while(!feof(f))
	{
		while(!feof(f))
		{
			fgets(buf,sizeof(buf),f);
			if(buf[0] > ' ')
				break;
		}

		if(buf[0] <= ' ')
			break;

		buf[strlen(buf)-1]=0;   /* delete /n at end of line */
		sscanf(buf,"%s",&cat_name);

		fgets(buf,sizeof(buf),f);
		i = strlen(buf);
		buf[i-1] = 0;
		strcpy(cat_comment,&buf[1]);

		fgets(buf,sizeof(buf),f);
		i = strlen(buf);
		buf[i-1] = 0;
		strcpy(cat_hierarchy,&buf[1]);

		if(pass == 1)
		{
			if(category_lookup(fr,cat_name,0) > 0)
				continue;   /* already exists */

			cat = category_alloc(fr,0);
			cptr = &fr->cat_data[cat];

			category_add_chars(fr,cat_name,&cptr->name);
			category_add_chars(fr,cat_comment,&cptr->comment);
			cptr->right = chain;
			chain = cat;
		}
		else if(cat_hierarchy[0] != 0)
		{
			/* look at hierarchy information and re-link into specified place */
			p = cat_hierarchy;
			relation = 0;
			i = 0;

			do
			{
				c = *p++;

				if((c == 0) || (strchr("></ ",c) != 0))
				{
					cat_name2[i] = 0;

					if(c == '>')
					{
						relation = 1;
					}
					else if(c == '<')
					{
						relation = 2;
					}

					if(i > 0)
					{
						cat = category_lookup(fr,cat_name,0);
						cat2 = category_lookup(fr,cat_name2,0);

						if((cat > 0) && (cat2 > 0))
						{
							if(relation == 1)
							{
								/* cat is parent of cat2 */
								category_unlink(fr,cat2);
								category_link(fr,cat2,1,cat,0);
							}
							else if(relation == 2)
							{
								/* cat is child of cat2 */
								category_unlink(fr,cat);
								category_link(fr,cat,1,cat2,0);
							}
						}
					}
					i=0;
				}
				else
				{
					cat_name2[i++] = c;
				}
			} while(c != 0);
		}
	}

	if(pass == 1)
	{
		/* link into the category structure, reverses the order they were put into 'chain' */
		while((cat = chain) != 0)
		{
			chain = fr->cat_data[cat].right;
			fr->cat_data[cat].right = 0;

			category_link(fr,cat,1,parent,0);
		}
	}
	else
	{
		category_set_positions(&src_fr);
		category_sort(fr,parent);
	}
}   /* end of import_cats */




void import_sources(FOLDREC *fr, FILE *f)
/***************************************/
{
	int  i;
	int  cat;
	int  parent;
	CAT *cptr;
	int  chain=0;
	char cat_name[20];
	char cat_comment[128];
	char buf[128];


	parent = 0;
	if(query2(query_parent,"Yes","No")!=0)
	{
		strcpy(buf,"KO2");  /* needed because of program overlays */
		parent = category_lookup(fr,buf,0);
		if(parent == 0)
		{
			category_add_child(fr,buf,NULL,&parent,0);
			category_sort(fr,0);
		}
	}

	/* skip header lines */
	for(i=0; i<3; i++)
	{
		fgets(buf,sizeof(buf),f);
	}

	while(!feof(f))
	{
		if(fgets(buf,sizeof(buf),f)==NULL)
			break;

		if(buf[0] > ' ')
		{
			buf[strlen(buf)-1]=0;   /* delete /n at end of line */

			sscanf(buf,"%s",cat_name);

			if(category_lookup(fr,cat_name,0) > 0)
				continue;   /* already exists */

			strcpy(cat_comment,&buf[12]);

			cat = category_alloc(fr,0);
			cptr = &fr->cat_data[cat];

			category_add_chars(fr,cat_name,&cptr->name);
			category_add_chars(fr,cat_comment,&cptr->comment);
			cptr->right = chain;
			chain = cat;
		}
	}

	/* add to source list in reverse order */
	while((cat = chain) != 0)
	{
		chain = fr->cat_data[cat].right;
		fr->cat_data[cat].right = 0;

		category_link(fr,cat,1,parent,0);
	}

	category_set_positions(&src_fr);
	category_sort(fr,parent);
}   /* end of import_sources */



void import_cats_file(char *filename, int filetype)
/*********************************************/
{
	int  i;
	int  displ;
	FILE *f;
	FOLDREC *fr;
	char buf[80];

	f = fopen(filename,"r");
	if(f == NULL)
		return;

	fgets(buf,sizeof(buf),f);
	if(strcmp(buf,"KO2 AUTO TEXT ITEMS\n") ==0)
	{
		import_auto_text(f,filename);
		return;
	}

	if(memcmp(buf,"  KO2 Subject :",15)!=0)
	{
		fclose(f);
		return;
	}

	for(i=0; i<3; i++)
	{
		fgets(buf,sizeof(buf),f);
	}

	if(strcmp(buf,"Contexts\n")==0)
	{
		fr = &cty_fr;

		displ = (int)ftell(f);
		import_cats(fr,f,1);
		fr->cat_data[0].down = fr->cat_data[0].right;

		fseek(f,displ,SEEK_SET);
		import_cats(fr,f,2);
		save_catfile(fr,1);
		wimp_close_wind(fr->window);
	}
	else if(strcmp(buf,"Sources\n")==0)
	{
		fr = &src_fr;

		import_sources(fr,f);
		save_catfile(fr,1);
		wimp_close_wind(fr->window);
	}


	fclose(f);
}   /* end of import_cats_file */





/******************************************************************/
/*                     IMPORT FROM VOYAGER                        */
/******************************************************************/

#ifdef ARGO_IMPORT

extern int get_box_number(char *name);

extern BOX box_table[N_BOXES];



char *read_basic_string(FILE *f)
/******************************/
{
	int  c;
	int  length;
	static char buf[256];

	buf[0] = 0;

	c = fgetc(f);
	if(feof(f))
		return(buf);

	length = fgetc(f);

	buf[length] = 0;
	while(length-- > 0)
	{
		c = fgetc(f);
		buf[length] = c;
	}
	return(buf);
}   /* end of read_basic_string */


int read_basic_int(FILE *f)
/*************************/
{
	int  i;
	int acc = 0;
	int tag;
	tag = fgetc(f);

	for(i=0; i<4; i++)
	{
		acc = acc << 8;
		acc += fgetc(f);
	}
	return(acc);
}   /* end of read_basic_int */



void argo_address_book()
/**********************/
{
	int  format;
	FILE *f;
	FILE *f_out;
	int  colr;
	int  len;
	char *p;
	char fname[80];
	char dirname[48];
	int dir_buf[12];
	ADDR_BOOK2 addr;
	os_regset regs;
	os_error *error;

	static colr_trans[] = {0,7,7,7,7,7,7,7, 6,3,4,1,3,7,2,5};
	/* read address book */
	sprintf(fname,"<PostyUser$Dir>.Addresses");
	f = fopen(fname,"r");
	if(f != NULL)
	{
		if(strcmp(read_basic_string(f),"@Frmt:2")==0)
			format=2;
		else
		{
			rewind(f);
			format=1;
		}

		while(!feof(f))
		{
			memset(&addr,0,sizeof(addr));
			strcpy(addr.data,read_basic_string(f));

			addr.alias = strlen(addr.data)+1;
			addr.offset = addr.alias + 1;

			p = &addr.data[addr.offset];
			strcpy(p,read_basic_string(f));
			len = strlen(p);

			if(feof(f))
				break;

			if(format==2)
				colr = read_basic_int(f);
			else
				colr = 0;
			addr.colr = colr_trans[colr & 0xf];
			addr.length = sizeof(ADDR_BOOK) + addr.offset + len + 1;

			if(addr_list_lookup_name(addr.data,0,-1,0) >= 0)
				continue;

			addrlist_add_entry(&addr,1);
		}
		fclose(f);
		save_addr_file();
	}

	/* distribution lists */
	sprintf(dirname,"<PostyUser$Dir>.MailList");

	regs.r[0] = 10;
	regs.r[1] = (int)dirname;
	regs.r[2] = (int)dir_buf;
	regs.r[3] = 1;
	regs.r[4] = 0;
	regs.r[5] = sizeof(dir_buf);
	regs.r[6] = 0;

	while(regs.r[3] > 0)
	{
		error = os_swix(0x0c,&regs);           /* OS_GBPB 10, read directory entries */
		if((error != NULL) || (regs.r[3] == 0))
			break;

		p = (char *)&dir_buf[5];
		sprintf(fname,"%sChoices.Maillists.%s",pluto_path,p);
		f_out = fopen(fname,"w");
		if(f_out == NULL)
			continue;

		sprintf(fname,"%s.%s",dirname,p);

		f = fopen(fname,"r");
		if(f == NULL)
		{
			fclose(f_out);
			continue;
		}

		fprintf(f_out,"!To:\nDistribution List\n!Bcc:\n");

		while(!feof(f))
		{
			p = read_basic_string(f);
			if(!feof(f))
			{
				fprintf(f_out,"%s\n",p);
			}
		}
		fclose(f);
		fclose(f_out);

	}
}   /* end of argo_address_book */



static void argo_import_box(char *name, char *ufile, int type, OPTIONS_MAILBOX *user, int n_users)
/************************************************************************************************/
{
	int  c;
	FILE *f;
	FILE *f_out;
	char *fname_temp;
	int  flags;
	int  box;
	int  box_standard;
	int  common_box;
	BOX *b;
	char box_name[80];
	char buf[80];
	static char *types[] = {"Standard","Save","Log"};
	static char *types2[] = {"Mail","Store","Log"};

	if((n_users>2) && (type>0))
	{
		/* use a common Log and Save box */
		sprintf(box_name,"%s Emails",types2[type]);
		common_box=1;

		/* rename Mail 1 Store and Mail 1 Log */
		sprintf(buf,"Mail 1 %s",types2[type]);
		box = get_box_number(buf);
		if((box >= 0) && (box < BOX_BIN))
		{
			b = &box_table[box];
			strcpy(b->name,box_name);
			b->hide_box = 0;
			b->colour = 4;   /* green */
			b->sort_on = 9;   /* Sort on SOURCE */
		}
	}
	else
	{
		sprintf(box_name,"%s %s",types2[type],name);
		common_box=0;
	}
	box_name[sizeof(box_table[0].name)-1] = 0;

	switch(type)
	{
	case 0:
		box = user->box;
		break;
	case 1:
		box_standard = user->box;
		box = box_table[box_standard].archive_to;
		break;
	case 2:
		box = user->log;
		break;
	}

	if(box >= BOX_BIN)
	{
		box = get_box_number(box_name);
	}

	if((box < 0) || (box >= BOX_BIN))
	{
		/* box doesn't exist.  create it */
		for(box=0; box<BOX_BIN; box++)
		{
			b = &box_table[box];
			if(b->name[0] == 0)
			{
				memset(b,0,sizeof(*b));
				b->sort_on = 1;  /* sort on date */
				b->archive_to = 255;
				b->expire_to = BOX_BIN;
				b->position = box;

				if((type != 0) && (common_box==0))
				{
					b->indent=1;
					b->hide_box=1;
				}

				if((type > 1) && (common_box))
				{
					b->sort_on = 9;  /* Sort on SOURCE */
				}

				if(type == 2)
				{
					/* "log" box */
					b->colour = 4;   /* green */
					b->colour_og = 4;
				}
				break;
			}
		}
		if(box==BOX_BIN)
		{
			werr(0,"Can't create Box '%s'. Number of Boxes limit reached",box_name);
			return;
		}
	}
	else
	{
		b = &box_table[box];
	}

	strcpy(b->name,box_name);

	if(type == 0)
	{
		/* set User Main In box */
		user->box = box;
	}
	else if(type == 2)
	{
		/* "log" box */
		b->colour_og = 4;

		/* set User Log Box */
		user->log = box;
	}
	else if(type == 1)
	{
		/* "save box" */
		if(box_standard < BOX_BIN)
			box_table[box_standard].archive_to = box;
	}


	sprintf(buf,"<PostyUser$Dir>.MailBoxes.%s.%s",types[type],ufile);
	f = fopen(buf,"r");
	if(f==NULL)
		return;

	sprintf(buf,"Import emails from '%s' %s box?",name,types[type]);
	if(query2(buf,"Import","No")==0)
	{
		fclose(f);
		return;
	}

	fname_temp = get_temp_fname();
	f_out = fopen_werr(fname_temp,"w",NULL);
	if(f_out==NULL)
	{
		fclose(f);
		return;
	}

	while(!feof(f))
	{
		c = fgetc(f);
		if(feof(f))
			break;
		fputc(c ^ 0x21,f_out);
	}
	fclose(f);
	fclose(f_out);

	flags = 0x3003;
	if(type == 2)
		flags |= 0x10;  /* log of outgoing messages */
	import_articles2(fname_temp,X_MAIL,box,flags);

	set_boxlist_extent(3);   /* display boxlist */
	remove(fname_temp);
}   /* end of argo_import_box */



int argo_get_config_string(char *label,char *out,int max)
/*******************************************************/
{
	int *i;
	os_regset regs;

	i = (int *)label;
	regs.r[0] = i[0];
	regs.r[1] = i[1];
	regs.r[2] = (int)out;
	regs.r[3] = max;
	os_swix(0x842c0,&regs);   /* VTiInternet_ReadConfigure */
	return(regs.r[0]);
}   /* end of argo_get_config_string */



void argo_change_run(char *name)
/******************************/
{
	FILE *f;
	char fname[48];
	char fname2[48];
	char buf[128];

	sprintf(fname,"<VTiInternet$Dir>.Apps.%s.!Run",name);
	f = fopen_werr(fname,"r",NULL);
	if(f != NULL)
	{
		fgets(buf,sizeof(buf),f);
		fclose(f);
		if(strstr(buf,"Pluto")==NULL)
		{
			/* don't copy Run file to Run_old if it is already a Pluto run file! */
			sprintf(fname2,"%s_old",fname);
			rename(fname,fname2);
		}
	}

	f = fopen_werr(fname,"w",NULL);
	if(f==NULL)
		return;

	fprintf(f,"| Run Pluto for %s\nRun %s!Run -%s\n",name,pluto_path,name);
	fclose(f);
	set_filetype(fname,0xfeb);  /* Obey file */
}   /* end of argo_change_run */




int argo_read_user(FILE *f_users, char *uname, char *realname, char *ufile, int version)
/**************************************************************************************/
{
	int  i;

	strcpy(uname,read_basic_string(f_users));
	if(feof(f_users))
		return(-1);

	strcpy(realname,read_basic_string(f_users));
	strcpy(ufile,read_basic_string(f_users));

	switch(version)
	{
	case 1:
		for(i=0; i<2; i++)
			read_basic_int(f_users);
		break;

	case 2:
		for(i=0; i<3; i++)
			read_basic_int(f_users);
		break;

	case 3:
		for(i=0; i<3; i++)
			read_basic_int(f_users);

		read_basic_string(f_users);  /* password */
		break;
	}
	return(0);
}   /* end of argo_read_user */




void argo_import()
/****************/
{
	int  i;
	int  ix;
	FILE *f_users;
	int  box;
	OPTIONS_MAILBOX *m;
	int  free_ix;
	char *p;
	int  n_users;
	int  user_num;
	int  version;
	int  found;
	int  parent;
	long int displ;
	char version_str[4];
	char uname[80];
	char *udomain;
	char realname[80];
	char ufile[16];
	char buf[128];
	static int voyptr[] = {X_VOYAGER};

	if(query("Import user data from Voyager ?",NULL)==0)
		return;

	if(read_os_variable("INNewsIn$File",options.news_in,sizeof(options.news_in)-1) == 1)
	{
		err_please_run("Voyager");
		return;
	}

	dbox_mail_provider_selected(voyptr);
	dbox_news_provider_selected(voyptr);

	argo_get_config_string("USERDOMN",options.domain,sizeof(options.domain));
	argo_get_config_string("USERACNT",options.u_name,sizeof(options.u_name));
	argo_get_config_string("USERMAIL",options.mailbox[0].email_addr,sizeof(options.mailbox[0].email_addr));
	argo_get_config_string("USERREAL",options.mailbox[0].full_name,sizeof(options.mailbox[0].full_name));
	p = strchr(options.mailbox[0].email_addr,'@');
	if(p != NULL)
		options.mailbox[0].user_id_len = p - options.mailbox[0].email_addr;

	f_users = fopen("<PostyUser$Dir>.Users","r");
	if(f_users==NULL)
	{
		werr(0,"Run then Quit the Voyager Mail program in order to set the PostyUser$Dir system variable");
		return;
	}

	strcpy(version_str,read_basic_string(f_users));
	if(strcmp(version_str," #2")==0)
		version = 2;
	else if(strcmp(version_str," #3")==0)
		version = 3;
	else
	{
		version = 1;
		rewind(f_users);
	}


	/* count number of users in Posty */
	displ = ftell(f_users);
	n_users = 0;
	while(!feof(f_users))
	{
		if(argo_read_user(f_users,uname,realname,ufile,version) < 0)
			break;
		n_users++;
	}
	fseek(f_users,displ,SEEK_SET);


	user_num = 0;
	while(!feof(f_users))
	{
		if(argo_read_user(f_users,uname,realname,ufile,version) < 0)
			break;


		user_num++;

		/* add User */
		free_ix = -1;
		if((udomain = strchr(uname,'@')) != NULL)
		{
			/* user name includes a domain */
			*udomain++ = 0;
		}
		else
		{
			udomain = options.domain;  /* use default domain */
		}

		/* set up a Source for this User */
		sprintf(buf,"%s@%s",uname,udomain);
		strcpy_lc(buf,buf);
		category_add_mail_name(buf,0);

		sprintf(buf,"%s@%s",uname,udomain);
		found = 0;
		for(ix=0; ix<N_MAIL_BOX; ix++)
		{
			m = &options.mailbox[ix];
			if(m->email_addr[0] == 0)
			{
				if(free_ix == -1)
					free_ix = ix;
				continue;
			}
			if(strcmp_lc(buf,m->email_addr)==0)
			{
				free_ix = ix;
				found = 1;
				break;
			}
		}
		if(free_ix >= 0)
		{
			m = &options.mailbox[free_ix];

			if(found == 0)
			{
				memset(m,0,sizeof(*m));
				m->box = BOX_BIN;
				m->log = BOX_BIN;
			}
			strcpy(m->email_addr,buf);
			m->user_id_len = strlen(uname);
			realname[sizeof(m->full_name)-1]=0;
			strcpy(m->full_name,realname);
		}

		for(i=0; i<3; i++)
			argo_import_box(uname,ufile,i,m,n_users);

		strcpy(realname,"<VTiInternet$Dir>.User.Signature");
		sprintf(buf,"<VTiInternet$Dir>.User.Sig%d",user_num);
		if(get_filelength(buf)>0)
			strcpy(realname,buf);
		sprintf(buf,"<PostyUser$dir>.Sigs.%s",ufile);
		if(get_filelength(buf)>0)
			strcpy(realname,buf);

		strncpy0(m->signature,make_legal_fname(uname),sizeof(m->signature));
		sprintf(buf,"%s.Signatures.%s",path_choices,m->signature);
		file_copy(realname,buf);
	}
	fclose(f_users);

	check_multiple_domains();

	make_boxes_menu(0,NULL);
	options_save();
	options_load();

	mbox_open(3);

	/* import news articles */
	if(query2("Import news articles?","Import","No")!=0)
	{
		box = get_box_number("News In");
		if(box >= 0)
		{
			import_articles2("<VTiInternet$Dir>.Apps.News.!NewsAgent.Articles",0x1000,box,0);
		}
	}

	save_boxes_file();
	load_boxes_file();
	set_boxlist_extent(3);

	parent = category_lookup(&src_fr,"mail",0);
	category_sort(&src_fr,parent);
	save_catfile(&src_fr,1);

	/* change news and mail run files */
	if(query2("Change News.!Run and Mail.!Run inside !Voyager so that Voyager News and Mail buttons run Pluto?","Yes","No")!=0)
	{
		argo_change_run("news");
		argo_change_run("mail");
	}
	argo_address_book();

}   /* end of argo_import */


#else
void argo_import()
/****************/
{}

#endif


